package cn.com.surker.weight.service.impl;

import cn.com.surker.util.StringUtils;
import cn.com.surker.weight.dto.WeightStorageLogDto;
import cn.com.surker.weight.entity.WeightStorageLog;
import cn.com.surker.weight.dao.WeightStorageLogDao;
import cn.com.surker.weight.service.WeightStorageLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import cn.com.surker.spring.RestResultDto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
* <p>
    * 仓位日志 服务实现类
    * </p>
*
* @author admin
* @since 2025-05-12
*/
@Service
public class WeightStorageLogServiceImpl extends ServiceImpl<WeightStorageLogDao, WeightStorageLog> implements WeightStorageLogService {

    @Resource
    private WeightStorageLogDao storageLogDao;


    /**
     * 分页查询数据
     * @param dto
     * @return
     */
    public IPage findByPage(WeightStorageLogDto dto, Integer offset, Integer limit) {
        if (StringUtils.isNull(offset))
            offset = 0;
        if (StringUtils.isNull(limit))
            limit = 15;
        PageHelper.offsetPage(offset, limit);
        QueryWrapper<WeightStorageLog> query = new QueryWrapper<>();
        query.eq("status", 0);
        if (!StringUtils.isNull(dto.getOrgId()))
            query.eq("org_id", dto.getOrgId());
        if (!StringUtils.isNull(dto.getType()))
            query.eq("type", dto.getType());
        if (!StringUtils.isNull(dto.getStartTime()))
            query.ge("create_time", dto.getStartTime());
        if (!StringUtils.isNull(dto.getEndTime()))
            query.lt("create_time", dto.getEndTime());
        if (!StringUtils.isNull(dto.getSearchVal()))
            query.and(orderWrapper -> orderWrapper.like("storage", dto.getSearchVal()).or()
                    .like("order_id", dto.getSearchVal()).or().like("remark", dto.getSearchVal())
                    .or().like("creator", dto.getSearchVal()));
        query.orderByDesc("id");
        IPage<WeightStorageLog> iPage = storageLogDao.selectPage(new Page<>((offset + limit) /limit, limit), query);
        /**分页插件**/
        if (!StringUtils.isNull(iPage.getRecords())) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal()))
                iPage.setTotal(pageInfo.getTotal());
        }
        return iPage;
    }
}